import pymongo
"""
该类连接数据库，操作数据库
class Mongo(dbs, collect)，连接参数dbs数据库,连接参数collect集合

方法1  def insertdict(mydict) 插入mydict文档
方法2   def finddict(self,id): 读取某个值
"""

class Mongo:
    # 连接参数dbs数据库,连接参数collect集合\

    def __init__(self, dbs, collect):
        self.myclient = pymongo.MongoClient("mongodb://localhost:27017/")
        self.mydb = self.myclient[dbs]
        self.mycol = self.mydb[collect]

    """插入mydict文档"""
    def insertdict(self, mydict):
        try:
            x = self.mycol.save(mydict)
        except:print("插入失败")
        else:print("插入成功")

    """读取某个值"""
    def finddict(self,id):
        onefind =self.mycol.find_one(id)
        return onefind

    """读取数据库的长度"""
    def collCount(self):
        count=self.mycol.estimated_document_count() # 返回集合数量简写  ,上方为自
        return int(count)

    """ 插入工作量 和材料"""
    def collset(self,n,work,matarl,GD49):  #插入工作量 和材料
        myquery = {"_id": n}
        newvalues = {"$set": {"workload": work}}
        newvalues2 = {"$set": {"material": matarl}}
        newvalues3 = {"$set": {"GD49": GD49}}
        self.mycol.update_many(myquery,newvalues)
        self.mycol.update_many(myquery, newvalues2)
        self.mycol.update_many(myquery, newvalues3)
        print(str(n)+"修改成功")

    """ 处理价格台班说明"""
    def collsetAmotunt(self,nn,prince,GD49,SM):  #价格#GD49
       myquery = {"_id": nn}
       prince=prince
       GD49=GD49
       newvalues4 = {"$set": {"shuoming": SM}}
       self.mycol.update_many(myquery, newvalues4)
       if prince<=500:
           newvalues = {"$set": {"jiage": prince}}
           newvalues1 = {"$set": {"TBjiage":500}}
           newvalues2 = {"$set": {"TaiBan": "台班"}}
           self.mycol.update_many(myquery, newvalues)
           self.mycol.update_many(myquery, newvalues1)
           self.mycol.update_many(myquery, newvalues2)
       elif prince>=500:
           if GD49 =="true":
               yjstr="窨井1x"+str(int(prince-200))+"="+str(int(prince-200))+"人工1x200=200"
               newvalues = {"$set": {"jiage": prince}}
               newvalues1 = {"$set": {"TBjiage": prince}}
               newvalues2 = {"$set": {"TaiBan": yjstr}}
               self.mycol.update_many(myquery, newvalues)
               self.mycol.update_many(myquery, newvalues1)
               self.mycol.update_many(myquery, newvalues2)
           elif GD49 =="false":
               newvalues = {"$set": {"jiage": prince}}
               newvalues1 = {"$set": {"TBjiage": prince}}
               newvalues2 = {"$set": {"TaiBan": ""}}
               self.mycol.update_many(myquery, newvalues)
               self.mycol.update_many(myquery, newvalues1)
               self.mycol.update_many(myquery, newvalues2)

